﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SatisKarsilastirma
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void programAyarlarıToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAyarlar ayar = new frmAyarlar();
            ayar.ShowDialog();
        }

        private void mağazaTanımlarıToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMagazaTanitim magaza = new frmMagazaTanitim();
            magaza.ShowDialog();
        }

        List<Depo> depolar;
        private void frmMain_Load(object sender, EventArgs e)
        {
            depolar = new Mikro().ListDepo();
            foreach (Depo item in depolar)
            {
                cmbDepo.Properties.Items.Add(item.Adi, false);
            }
        }

        private bool secilimi(string adi)
        {
            for (int i = 0; i < cmbDepo.Properties.Items.Count; i++)
            {
                if (cmbDepo.Properties.Items[i].CheckState == CheckState.Checked)
                {
                    if (cmbDepo.Properties.Items[i].Value.ToString() == adi)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //Aktarim aktarim = new Aktarim();
                List<Rapor> liste = new List<Rapor>();
                //foreach (Depo item in depolar)
                //{
                //    rpr.Add(aktarim.OncekiGunSatisi(new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day), item));
                //}

                Genius aktarim = new Genius();
                foreach (Depo item in depolar)
                {
                    if (secilimi(item.Adi))
                    {
                        Rapor rpr = aktarim.OncekiGunSatisi(new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day), item);
                        rpr.MikroSatisTutari -= rpr.MikroIadeTutari;
                        rpr.SatisFark = rpr.GeniusSatisTutari - rpr.MikroSatisTutari;
                        rpr.IadeFark = rpr.GeniusIadeTutari - rpr.MikroIadeTutari;
                        rpr.BelgeFark = rpr.GeniusBelgeSayisi - rpr.MikroBelgeSayisi;
                        rpr.IadeBelgeFarki = rpr.GeniusIadeSayisi - rpr.MikroIadeSayisi;
                        liste.Add(rpr);
                    }
                }

                dataGridView1.DataSource = liste;
                dataGridView1.AutoResizeColumns();
                dataGridView1.Refresh();
                Cursor.Current = Cursors.Default;
            }
            catch (Exception)
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = string.Format("SELECT     {0}.dbo.DEPOLAR.dep_adi AS Mağaza, MAGAZA_OZET.SatisBelgeAdedi AS [Fiş Adedi], MAGAZA_OZET.SatisTutari AS [Satış Tutarı],MAGAZA_OZET.IadeBelgeAdedi AS [Iade Adedi], MAGAZA_OZET.IadeBelgeTutari AS [Iade Tutarı], MAGAZA_OZET.NetSatisTutari AS [Net Tutar] FROM         MAGAZA_OZET WITH (NOLOCK) INNER JOIN {0}.dbo.DEPOLAR ON MAGAZA_OZET.MagazaNo = {0}.dbo.DEPOLAR.dep_no WHERE     (MAGAZA_OZET.Tarih = CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 102), 102)) order by {0}.dbo.DEPOLAR.dep_adi", Sessions.Get[Settings.DatabaseName].ToString().Replace("_WORKDATA", ""));
            using (SqlConnection cn = new SqlConnection(Sessions.Get.CnnString))
            {
                cn.Open();
                using (SqlDataAdapter adp = new SqlDataAdapter(sql, cn))
                {
                    adp.SelectCommand.CommandTimeout = 99999;
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    SubTotal(dataGridView2, dt);
                }
                cn.Close();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (SqlConnection cn = new SqlConnection(Sessions.Get.CnnString))
            {
                cn.Open();
                using (SqlDataAdapter adp = new SqlDataAdapter("exec dbo.AylikCiro", cn))
                {
                    adp.SelectCommand.CommandTimeout = 9999999;
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    SubTotal(dataGridView3, dt);
                }
                cn.Close();
            }
        }

        private void SubTotal(DataGridView grd, DataTable dt)
        {
            dt.Rows.Add();
            object[] dizi = new object[dt.Columns.Count];
            dizi[0] = "Toplam";

            for (int c = 0; c < dt.Columns.Count; c++)
            {
                if (dt.Columns[c].DataType != typeof(string))
                {
                    decimal toplam = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i][c] != DBNull.Value)
                        {
                            toplam += Cevir.Parse<decimal>(dt.Rows[i][c]);
                        }
                    }
                    dizi[c] = toplam;
                }
            }

            dt.Rows.Add(dizi);
            grd.DataSource = dt;

            foreach (DataGridViewColumn item in grd.Columns)
            {
                if (item.ValueType == typeof(decimal) || item.ValueType == typeof(double))
                {
                    item.DefaultCellStyle.Format = "C2";
                }
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (SqlConnection cn = new SqlConnection(Sessions.Get.CnnString))
            {
                cn.Open();
                string tarih = dateTimePicker2.Value.Year.ToString() + dateTimePicker2.Value.Month.ToString("00") + dateTimePicker2.Value.Day.ToString("00");
                using (SqlDataAdapter adp = new SqlDataAdapter(string.Format("exec dbo.AnaGrupCiro @tarih='{0}'", tarih), cn))
                {
                    adp.SelectCommand.CommandTimeout = 9999999;
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    SubTotal(dataGridView4, dt);
                }
                cn.Close();
            }
        }
    }
}
